package admin_category;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Include_navigation extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled6() throws Exception {
	Actions actions = new Actions(driver);
    driver.manage().window().maximize();
    driver.get(baseUrl + "admin/");
    /*driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.id("login")).clear();
    driver.findElement(By.id("login")).sendKeys("admin123");
    driver.findElement(By.cssSelector("input.form-button")).click();
    driver.findElement(By.cssSelector("a[title=\"close\"] > span")).click();*/
    actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[3]/a/span"))).build().perform(); 
    driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li[2]/a/span")).click();
    //driver.findElement(By.id("ext-gen41")).click();
    driver.findElement(By.id("extdd-18")).click();
    TimeUnit.SECONDS.sleep(5);
    new Select(driver.findElement(By.id("group_7include_in_menu"))).selectByVisibleText("No");
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.cssSelector("button[title=\"Save Category\"]")).click();
    TimeUnit.SECONDS.sleep(5);
    driver.get(baseUrl);
    actions.moveToElement(driver.findElement(By.cssSelector("li.level0.nav-3.last.level-top.parent"))).build().perform();
    TimeUnit.SECONDS.sleep(3);
    assertEquals("Product listing 2", driver.findElement(By.cssSelector("li.level1.nav-3-4.last > a > span")).getText());
    driver.get(baseUrl + "furniture/test-category.html");
    assertEquals("Test category", driver.findElement(By.cssSelector("h1")).getText());
    driver.get(baseUrl + "admin/");
    TimeUnit.SECONDS.sleep(3);
    actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[3]/a/span"))).build().perform();
    driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li[2]/a/span")).click();
    driver.findElement(By.id("extdd-18")).click();
    TimeUnit.SECONDS.sleep(3);
    new Select(driver.findElement(By.id("group_7include_in_menu"))).selectByVisibleText("Yes");
    driver.findElement(By.cssSelector("button[title=\"Save Category\"]")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.get(baseUrl);
    actions.moveToElement(driver.findElement(By.cssSelector("li.level0.nav-3.last.level-top.parent"))).build().perform();
    TimeUnit.SECONDS.sleep(3);
    assertEquals("Test category", driver.findElement(By.cssSelector("li.level1.nav-3-5.last > a > span")).getText());
    actions.moveToElement(driver.findElement(By.cssSelector("li.level0.nav-3.last.level-top.parent"))).build().perform();
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.cssSelector("li.level1.nav-3-5.last > a > span")).click();
    assertEquals("Test category", driver.findElement(By.cssSelector("h1")).getText());    
    //driver.quit();
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
